#include <iostream>
using namespace std;

class Person {
  public:
    Person(const char* name, int age) {
      strcpy(this->name, name);
      this->age = age;
    }
    ~Person() {
      cout << "Person 析构" << endl;
    }
    void print() {
      cout << "name: " << name << " age: " << age << endl;
    }
    
  private:
    int age;
    char name[30];
};

void test() {
  Person p("Mango", 18);
  p.print();

  // 使用 malloc 手动分配空间，需要手动管理内存
  Person *pp = (Person *)malloc(sizeof(Person));
  memcpy(pp, &p, sizeof(Person));
  pp->print();
  delete(pp); // 调用析构，释放内存
  // free(pp)  // 不会调用析构，但会释放内存

  Person *pp2 = new Person("Mango2", 20);
   memcpy(pp2, &p, sizeof(Person));
  pp2->print();
  delete pp2; // 调用析构，释放内存
  // free(pp2);  // 不会调用析构，但会释放内存
}

int main() {
  test();
  return 0;
}
